package com.weishi;

import cn.hutool.http.HttpUtil;
import com.weishi.framework.config.SystemParameter;
import com.weishi.project.speedometer.domain.EmqMqttClient;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.logging.Logger;

/**
 * 启动程序
 */
@SpringBootApplication
@EnableScheduling
public class SupportApplication extends SpringBootServletInitializer {
    private static Logger logger = Logger.getLogger("SupportApplication");

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SupportApplication.class, args);

        //启动tcp监听
       /* TCPService tcpService = new TCPService();
        tcpService.run();*/
        //开启MQTT监听服务
        String mqttService = context.getEnvironment().getProperty("mqtt.service");
        String mqttPort = context.getEnvironment().getProperty("mqtt.port");
        String mqttClientId = context.getEnvironment().getProperty("mqtt.clientId");
        String mqttTopic = context.getEnvironment().getProperty("mqtt.topic");
        String mqttUser = context.getEnvironment().getProperty("mqtt.user");
        String mqttPass = context.getEnvironment().getProperty("mqtt.pass");
        EmqMqttClient emqMqttClient = EmqMqttClient.getInstance();
        boolean ret = emqMqttClient.init(mqttService, mqttPort, mqttUser, mqttPass, mqttClientId, mqttTopic);
        logger.info("====  MqttClient启动成功！ ====");


        /*String tcpService = context.getEnvironment().getProperty("tcp.service");
        if (tcpService.equals("enable")) {
            //启动tcp服务
            String tcpPort = context.getEnvironment().getProperty("tcp.port");
            TCPServercallBack tcpServercallBack = TCPServercallBack.getInstance();
            TCPServer tcpServer = TCPServer.getInstance();
            tcpServer.Start("0.0.0.0", Integer.parseInt(tcpPort), tcpServercallBack);
            tcpServer.SetHreat("zoulj", "{\"from\":\"client\",\"msgId\":\"heart_beat\"}", 10);
            //开启客户端回复
            tcpServercallBack.replyClient();
            logger.info("====  tcp启动成功！ ====");
        }*/


        logger.info("====  系统启动成功！ ====");

        //服务上报
        //serviceReport(context);
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(SupportApplication.class);
    }

    /**
     * 向总系统上报相关信息
     */
    private static void serviceReport(ConfigurableApplicationContext context) {
        SystemParameter systemParameter = new SystemParameter();
        try {
            String springProfilesActive = context.getEnvironment().getProperty("spring.profiles.active");
            systemParameter.setSpringProfilesActive(springProfilesActive);
            String mainSystemUrl = context.getEnvironment().getProperty("sys.mainSystemUrl");
            systemParameter.setSysMainSystemUrl(mainSystemUrl);
            String serverPort = context.getEnvironment().getProperty("server.port");
            systemParameter.setServerPort(Integer.parseInt(serverPort));
            String fileProfile = context.getEnvironment().getProperty("ruoyi.profile");
            systemParameter.setFileProfile(fileProfile);
            String tcpPort = context.getEnvironment().getProperty("tcp.port");
            systemParameter.setTcpPort(Integer.parseInt(tcpPort));
            String tcpService = context.getEnvironment().getProperty("tcp.service");
            systemParameter.setTcpService(tcpService);

            String mqttService = context.getEnvironment().getProperty("mqtt.service");
            systemParameter.setMqttService(mqttService);
            String mqttPort = context.getEnvironment().getProperty("mqtt.port");
            systemParameter.setMqttPort(Integer.parseInt(mqttPort));
            String mqttClientId = context.getEnvironment().getProperty("mqtt.clientId");
            systemParameter.setMqttClientId(mqttClientId);
            String mqttTopic = context.getEnvironment().getProperty("mqtt.topic");
            systemParameter.setMqttTopic(mqttTopic);
            String mqttUser = context.getEnvironment().getProperty("mqtt.user");
            systemParameter.setMqttUser(mqttUser);
            String mqttPass = context.getEnvironment().getProperty("mqtt.pass");
            systemParameter.setMqttPass(mqttPass);

            String redisHost = context.getEnvironment().getProperty("spring.redis.host");
            systemParameter.setRedisHost(redisHost);
            String redisPort = context.getEnvironment().getProperty("spring.redis.port");
            systemParameter.setRedisPort(Integer.parseInt(redisPort));
            String redispassword = context.getEnvironment().getProperty("spring.redis.password");
            systemParameter.setRedispassword(redispassword);
            String redisdatabase = context.getEnvironment().getProperty("spring.redis.database");
            systemParameter.setRedisdatabase(Integer.parseInt(redisdatabase));

            String mysqlUrl = context.getEnvironment().getProperty("spring.datasource.url");
            systemParameter.setMysqlUrl(mysqlUrl);
            String mysqlUsername = context.getEnvironment().getProperty("spring.datasource.username");
            systemParameter.setMysqlUsername(mysqlUsername);
            String mysqlPassword = context.getEnvironment().getProperty("spring.datasource.password");
            systemParameter.setMysqlPassword(mysqlPassword);

            HttpUtil.post(mainSystemUrl, BeanUtils.describe(systemParameter));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}